package com.zhiyizhen.handheld.component.workspace;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zhiyizhen.handheld.common.DataCache;
import com.zhiyizhen.handheld.common.ImageLoader;
import com.zhiyizhen.handheld.common.RxJavaUtil;
import com.zhiyizhen.handheld.common.view.BaseFragment;
import com.zhiyizhen.handheld.component.login.bean.LoginBean;
import com.zhiyizhen.handheld.component.workspace.adapter.MyWorkTimeAdapter;
import com.zhiyizhen.handheld.component.workspace.api.IWorkspaceService;
import com.zhiyizhen.handheld.component.workspace.bean.EmpInfo;
import com.zhiyizhen.handheld.component.workspace.bean.MySchedule;
import com.zhiyizhen.handheld.databinding.FragmentWorkspaceBinding;
import com.zhiyizhen.handheld.net.ServiceFactory;
import com.zhiyizhen.handheld.net.response.ResponseData;
import com.zhiyizhen.handheld.net.response.ResponseObserver;

import java.util.List;

/**
 * 工作台Tab
 */
public class WorkspaceFragment extends BaseFragment {
    private FragmentWorkspaceBinding binding;
    private MyWorkTimeAdapter adapter;
    private LoginBean loginBean;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = FragmentWorkspaceBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }

    private void initView() {
        RecyclerView mRecyclerView = binding.recyclerView;
        adapter = new MyWorkTimeAdapter(getContext());
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setAdapter(adapter);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        loginBean = DataCache.getLoginBean();
        getEmpInfo();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            getEmpInfo();
        }
    }

    private void getEmpInfo() {
        ServiceFactory.getService(IWorkspaceService.class)
                .getEmpInfo(loginBean.getToken(), loginBean.getClinicId())
                .compose(RxJavaUtil.mainSchedulers())
                .as(this.bindLifecycle())
                .subscribe(new ResponseObserver<>() {
                    @Override
                    protected void onSuccess(ResponseData<EmpInfo> responseData) {
                        if (responseData != null && responseData.getData() != null) {
                            setEmpInfo(responseData.getData());
                        }
                    }

                    @Override
                    protected void onFinally() {
                        super.onFinally();
                        getMySchedule();
                    }
                });
    }

    private void getMySchedule() {
        ServiceFactory.getService(IWorkspaceService.class)
                .getMySchedule(loginBean.getToken(), loginBean.getClinicId())
                .compose(RxJavaUtil.mainSchedulers())
                .as(this.bindLifecycle())
                .subscribe(new ResponseObserver<>() {
                    @Override
                    protected void onSuccess(ResponseData<List<MySchedule>> responseData) {
                        if (responseData != null && responseData.getData() != null) {
                            adapter.setData(responseData.getData());
                        }

                        if (adapter.getData().isEmpty()) {
                            binding.emptyView.setVisibility(View.VISIBLE);
                            binding.recyclerView.setVisibility(View.GONE);
                        } else {
                            binding.emptyView.setVisibility(View.GONE);
                            binding.recyclerView.setVisibility(View.VISIBLE);
                        }
                    }
                });
    }

    private void setEmpInfo(EmpInfo empInfo) {
        binding.doctorNameTv.setText(empInfo.getEmpName());
        binding.infoTv.setText(empInfo.getGenderString() + " " + empInfo.getTitle() + " " + empInfo.getDeptName());
        binding.noteTv.setText(empInfo.getNotes());
        ImageLoader.Option option = ImageLoader.Option.create();
        option.setCircleCrop(true);
        ImageLoader.load(getActivity(), empInfo.getAvatarUrl(), binding.avatarImg, option);
    }

}
